/*     */ package com.controller;

/*     */ 
/*     */

import com.domain.Book;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/*     */
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Controller
/*     */ @RequestMapping({"/books"})
/*     */ public class BookController
/*     */ {
/*     */   @Autowired
/*     */   private IBookService bookService;
/*     */   
/*     */   @RequestMapping({"/findAll"})
/*     */   public String findAll(Model model, @RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "size", defaultValue = "4") int size) throws Exception {
/*  34 */     if (size == 0) {
/*  35 */       size = 4;
/*     */     }
/*     */     
/*  38 */     List<Book> bookList = this.bookService.findAll(page, size);
/*  39 */     PageInfo pageInfo = new PageInfo(bookList);
/*  40 */     model.addAttribute("pageInfo", pageInfo);
/*     */     
/*  42 */     return "book/book-list";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/saveBook"})
/*     */   public String saveBook(Book book, Model model) throws Exception {
/*  54 */     List<Book> books = this.bookService.findByName(book.getName());
/*  55 */     if (books.size() == 0) {
/*  56 */       System.out.println("执行了保存");
/*  57 */       this.bookService.saveBook(book);
/*  58 */       System.out.println(book);
/*  59 */       return "redirect:/books/findAll";
/*     */     } 
/*     */     
/*  62 */     List<Book> bookList = new ArrayList<>();
/*  63 */     bookList.add(book);
/*  64 */     model.addAttribute("books", bookList);
/*  65 */     model.addAttribute("msg", "书名重复");
/*  66 */     return "book/book-add-error";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/toSaveBook"})
/*     */   public String toSaveBook() {
/*  77 */     return "book/book-add";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/delete"})
/*     */   public ModelAndView delete(@RequestParam(name = "id") Integer id, @RequestParam(name = "page") Integer page, @RequestParam(name = "size") Integer size) {
/*  88 */     this.bookService.deleteBook(id);
/*  89 */     this.bookService.updateBid(id);
/*  90 */     this.bookService.autoIncrement();
/*  91 */     ModelAndView mv = new ModelAndView();
/*  92 */     mv.setViewName("redirect:/books/findAll?page=" + page + "&size=" + size);
/*  93 */     return mv;
/*     */   }
/*     */ 
/*     */   
/*     */   @RequestMapping({"/toUpdate"})
/*     */   public String toUpdate(Integer id, Model model) {
/*  99 */     Book book = this.bookService.findById(id);
/* 100 */     System.out.println("跳到修改页面...");
/* 101 */     System.out.println(book);
/* 102 */     model.addAttribute("book", book);
/* 103 */     return "book/book-update";
/*     */   }
/*     */   
/*     */   @RequestMapping({"/update"})
/*     */   public ModelAndView update(Book book) throws Exception {
/* 108 */     ModelAndView mv = new ModelAndView();
/* 109 */     String name = book.getName();
/* 110 */     System.out.println("执行更新...");
/* 111 */     this.bookService.updateBook(book);
/* 112 */     mv.setViewName("redirect:/books/findByName?name=" + URLEncoder.encode(name, "UTF-8"));
/* 113 */     return mv;
/*     */   }
/*     */ 
/*     */   
/*     */   @RequestMapping({"/findByName"})
/*     */   public String findByName(@RequestParam(name = "name") String name, Model model) throws Exception {
/* 119 */     List<Book> bookList = this.bookService.findMoHu("%" + name + "%");
/* 120 */     if (bookList.size() == 0) {
/* 121 */       String message = "未找到";
/* 122 */       model.addAttribute("message", message);
/*     */     } 
/* 124 */     PageHelper.startPage(1, 4);
/* 125 */     PageInfo pageInfo = new PageInfo(bookList);
/* 126 */     model.addAttribute("pageInfo", pageInfo);
/* 127 */     return "book/book-list";
/*     */   }
/*     */ }


/* Location:              G:\项目\图书管理系统\library_system-1.0-SNAPSHOT.war!\WEB-INF\classes\com\controller\BookController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */